Swap Nodes in Pairs
Easy
Question
Given a linked list, swap every pair of nodes and return its head.
Input: head = [1 -> 2 -> 3 -> 4]
Output: [2 -> 1 -> 4 -> 3]
Input: head = [4 -> 6 -> 8]
Output: [6 -> 4 -> 8]
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
What should the resulting linked list look like after swapping its pairs of nodes? head = [0 -> 5 -> 10 -> 20 -> 35]
[0 -> 10 -> 20 -> 5 -> 35]
[0 -> 10 -> 5 -> 35 -> 20]
[5 -> 0 -> 10 -> 35 -> 20]
[5 -> 0 -> 20 -> 10 -> 35]
Take a moment to understand the problem and think of your approach before you start coding.